package com.redtree.air.db
{
	import com.redtree.air.db.Model;
	import com.redtree.air.db.Security;
	
	//import flash.events.EventDispatcher;

	public class Securities extends Model
	{		
		public const SECURITY_NON:int=1;
		public const SECURITY_PWD:int=2;
		public const SECURITY_SUP:int=10;
		
		/**
		 * 获得所有的安全级别
		 * @return 数组Security
		 */
		public function getSecurities(underSecurity:Boolean=true,justCurrentLevel:Boolean=false):Array
		{
			var sql:String = "select * from security";
			if( underSecurity )
				sql += " where level_id "+(justCurrentLevel?"=":"<=")+" "+currentSecurityLevel;
			if(Model.instance.query(sql))var result:Array = Model.instance.data;
			var resultAry:Array = new Array();

			for( var i:int=0;i<result.length; i++ ){
				resultAry.push(new Security(result[i]));
			}
			return resultAry;
		}
		/**
		 * 获得当前安全级别 以及以下的级别
		 * @return 特有字段的数组
		 */
		public function getSecuritiesAsArray(underSecurity:Boolean=true,justCurrentLevel:Boolean=false):Array
		{
			var sql:String = "select name as label, level_id as data, description from security";
			if( underSecurity )
				sql += " where level_id "+(justCurrentLevel?"=":"<=")+" "+currentSecurityLevel;
			var result:Array;
			if(Model.instance.query(sql))result = (Model.instance.data );
			return result;
		}
		/**
		 * 更改登录状态时 可看到的安全级别 可比当前级别高1
		 * @return 特有字段的数组
		 */
		public function getAvailSecuritiesAsArray(enlargeLevel:Boolean=true,justCurrentLevel:Boolean=false):Array
		{
			var availLevel:int = currentSecurityLevel;
			if(enlargeLevel)
			switch( currentSecurityLevel )
			{
				case SECURITY_NON:availLevel = SECURITY_PWD;break;
				case SECURITY_PWD:availLevel = SECURITY_SUP;break;
				case SECURITY_SUP:availLevel = SECURITY_SUP;break;				
			}

			var sql:String = "select name as label, level_id as data, description from security";
			sql += " where level_id "+(justCurrentLevel?"=":"<=")+" "+availLevel;
			
			var result:Array;
			if(Model.instance.query(sql))result = (Model.instance.data );
			return result;
		}
		/**
		 * 验证级别与密码是否匹配
		 */
		public function checkPassword(levelId:int, pwd:String):Boolean
		{
			var sql:String = "select password from security WHERE level_id="+levelId;
			var result:Array;
			if(Model.instance.query(sql))result = (Model.instance.data);
			if( result[0].password == pwd ) {
				currentSecurityLevel = levelId;
				return true;
			}
			return false;
		}
		/**
		 * 判断要变成的安全级别是否需要密码
		 */
		public function isNeedPassword( levelId:int ):Boolean
		{
			if( currentSecurityLevel < levelId ) return true;
			else{
				currentSecurityLevel = levelId;
				return false;
			}
		}
		public function getCurrentSecuritylevel():int
		{
			return currentSecurityLevel;
		}
		/**
		 * 修改等级密码
		 */
		public function setPassword( levelId:int, pwd:String ):Boolean
		{
			if( this.isNeedPassword(levelId) ) return false;
			var sql:String = 'update security set password="'+pwd+'" where level_id='+levelId;
			return Model.instance.query(sql);
			
		}
	}	
}